package work.cqhx.springapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.cqhx.springapi.pojo.Student;
import work.cqhx.springapi.returnjson.ReturnData;
import work.cqhx.springapi.returnjson.ReturnJson;
import work.cqhx.springapi.service.StudentService;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/queryStudentAll")
    public List<Student> queryStudentAll()
    {
        return studentService.queryStudentAll();
    }
    @GetMapping("/queryStudentAsc")
    public List<Student> queryStudentAsc(){
        return studentService.queryStudentAsc();
    }
    @GetMapping("/queryStudentDesc")
    public List<Student> queryStudentDesc(){
        return studentService.queryStudentDesc();
    }
    @GetMapping("/queryStudentMax")
    public Student queryStudentMax(){
        return studentService.queryStudentMax();
    }
    @GetMapping("/queryStudentMin")
    public Student queryStudentMin(){
        return studentService.queryStudentMin();
    }

    @GetMapping("/queryStudentCount")
    public ReturnJson queryStudentCount(){
        ReturnJson returnJson = new ReturnJson();
        returnJson.setCount(studentService.queryStudentCount());
        return returnJson;
    }

    @GetMapping("/queryRate")
    public ReturnJson queryRate(){
        ReturnJson returnJson = new ReturnJson();
        // 获取学生总数
        int studentCount = studentService.queryStudentCount();

// 获取各科合格人数
        int chineseQualified = studentService.queryChineseQualifiedCount();
        int mathQualified = studentService.queryMathQualifiedCount();
        int englishQualified = studentService.queryEnglishQualifiedCount();

// 合格总人次
        int totalQualified = chineseQualified + mathQualified + englishQualified;

// 总成绩项数量（每人每科一个）
        int totalItems = studentCount * 3;

// 计算合格率（避免除零）
        double rate = totalItems == 0 ? 0 : ((double) totalQualified / totalItems) * 100;

// 四舍五入保留两位小数
        int ratePercent = (int) Math.round(rate);
        returnJson.setCount(ratePercent);
        return returnJson;
    }

    @GetMapping("/queryClassesAverage")
    public ReturnData queryClassesAverage(){
        return studentService.queryClassesAverage();
    }

    @GetMapping("/queryClassesAll")
    public List<Object> queryClassesAll(){

        return studentService.queryClassesAll();
    }

    @GetMapping("/conditionalQueriesStudent")
    public List<Student> conditionalQueriesStudent(@RequestParam(value = "classes",required = false) String classes, @RequestParam(value = "name",required = false) String name){
        return studentService.conditionalQueriesStudent(classes, name);
    }

    @PostMapping("/addStudent")
    public ReturnJson addStudent(@RequestBody Student student){
        ReturnJson returnJson = new ReturnJson();
        if(student!=null){
            student.setScore(student.getChinese() + student.getMath() + student.getEnglish());
            if(studentService.addStudent(student)) {
            returnJson.setStatus(true);
            }
        }
        return returnJson;
    }



    @DeleteMapping("/deleteStudent")
    public ReturnJson deleteStudent(@RequestParam("id") Integer id){
        ReturnJson returnJson = new ReturnJson();
        if(id!=null){
            if(studentService.deleteStudent(id)) {
                returnJson.setStatus(true);
                return returnJson;
            }
        }
        returnJson.setStatus(false);
        return returnJson;
    }
    @DeleteMapping("/deleteStudentBatch")
    public ReturnJson deleteStudentBatch(@RequestBody List<Integer> ids){
        ReturnJson returnJson = new ReturnJson();
        if(ids!=null){
            if(studentService.deleteStudentBatch(ids)) {
                returnJson.setStatus(true);
                return returnJson;
            }
        }
        returnJson.setStatus(false);
        return returnJson;
    }

    @PutMapping("/updateStudent")
    public ReturnJson updateStudent(@RequestBody Student student) {
        ReturnJson returnJson = new ReturnJson();
        if (student != null) {
            student.setScore(student.getChinese() + student.getMath() + student.getEnglish());
            if (studentService.updateStudent(student)) {
                returnJson.setStatus(true);
                return returnJson;
            }
        }
        returnJson.setStatus( false);
        return returnJson;
    }


}
